package com.cskaoyan.service;

import com.cskaoyan.bean.MarketLog;
import com.cskaoyan.bean.MarketLogExample;
import com.cskaoyan.bean.MarketOrder;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketLogMapper;
import com.cskaoyan.mapper.MarketOrderMapper;
import com.cskaoyan.util.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.deploy.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author gatsby
 * @since 2022/10/12 15:48
 */
@Service
public class AdminLogServiceImpl implements AdminLogService {

    @Autowired
    MarketLogMapper marketLogMapper;

    @Autowired
    MarketOrderMapper marketOrderMapper;

    /**
     * 查询全部日志的业务逻辑
     *
     * @param basePageInfo
     * @param username
     * @return com.cskaoyan.bean.common.CommonData<com.cskaoyan.bean.MarketLog>
     * @author gatsby
     * @since 2022/10/12 15:52
     */
    @Override
    public CommonData<MarketLog> queryAllLogsLimit(BasePageInfo basePageInfo, String username) {
        //开启分页
        PageHelper.startPage(basePageInfo.getPage(), basePageInfo.getLimit());

        MarketLogExample example = new MarketLogExample();
        //添加排序的条件
        example.setOrderByClause(basePageInfo.getSort() + " " + basePageInfo.getOrder());
        example.setDistinct(true);
        MarketLogExample.Criteria criteria = example.createCriteria();
        if (!StringUtil.isEmpty(username)) {
            //添加模糊查询的条件
            criteria.andAdminLike("%"+username+"%");
        }

        //分页查询全部的管理员
        List<MarketLog> marketLogList = marketLogMapper.selectByExample(example);

        //获取分页后的一些数量信息
        PageInfo<MarketLog> pageInfo = new PageInfo<>(marketLogList);
        int pages = pageInfo.getPages();
        int total = (int)pageInfo.getTotal();
        CommonData<MarketLog> commonData = new CommonData<>();
        commonData.setList(marketLogList);
        commonData.setLimit(basePageInfo.getLimit());
        commonData.setPage(basePageInfo.getPage());
        commonData.setTotal(total);
        commonData.setPages(pages);


        return commonData;
    }

    /**
     * 添加日志的业务逻辑
 * @param marketLog
     * @return void
     * @author gatsby
     * @since 2022/10/12 23:28
     */
    @Override
    public void insertLog(MarketLog marketLog) {
        marketLogMapper.insertSelective(marketLog);
    }

    /**
     * 查询订单号
 * @param id
     * @return java.lang.String
     * @author gatsby
     * @since 2022/10/17 12:50
     */
    @Override
    public String queryOrderSnById(Integer id) {
        MarketOrder marketOrder = marketOrderMapper.selectByPrimaryKey(id);
        return marketOrder.getOrderSn();
    }
}
